#pragma once
#include <iostream>
#include <string>
#include <sys/types.h>
#include <sys/socket.h>
using namespace std;
class Util
{

public:
    static int ReadLine(int sock, string &out)
    {
        char ch = 'x';

        while (ch != '\n')
        {
            ssize_t s = recv(sock, &ch, 1, 0); // 每次读一个
            if (s > 0)
            {
                if (ch == '\r')
                {
                    recv(sock, &ch, 1, MSG_PEEK);
                    if (ch == '\n')
                    {
                        recv(sock, &ch, 1, 0);
                    }
                    else
                    {
                        ch = '\n';
                    }
                }
                out.push_back(ch);
            }
            else if (s == 0)
            {
                return 0; // 客户端关闭
            }
            else
            {
                return -1; // 出现错误
            }
        }
        return out.size();
    }
};